# -*- coding: utf-8 -*-
# @File     : serializers.py
# @Author   : bingjia
# @Time     : 2020/8/11 10:32
# @Desc     :

from operation.base.api import serializers

from . import models


class DefectSerializer(serializers.ModelSerializer):
    """
    缺陷序列化
    """
    defect = serializers.SerializerMethodField()
    project = serializers.SerializerMethodField()
    model_info = serializers.SerializerMethodField()
    repair_video = serializers.SerializerMethodField()
    repair_advice = serializers.SerializerMethodField()
    example_defect = serializers.SerializerMethodField()
    defect_image_type = serializers.SerializerMethodField()
    defect_description = serializers.SerializerMethodField()

    class Meta:
        model = models.Defect
        fields = ['id', 'name', 'defect', 'project',
                  'location', 'shoot_date', 'thermal', 'visible',
                  'defect_image', 'model_info', 'created_date',
                  'repair_video', 'repair_advice', 'example_defect',
                  'defect_image_type', 'defect_description']

    def get_defect(self, obj):
        return obj.defect.name

    def get_project(self, obj):
        return obj.project.name

    def get_model_info(self, obj):
        return obj.model_info.name

    def get_repair_video(self, obj):
        return obj.defect_type.video

    def get_repair_advice(self, obj):
        return obj.defect_type.advice

    def get_example_defect(self, obj):
        return obj.defect_type.image

    def get_defect_image_type(self, obj):
        return obj.defect_type.get_type_display()

    def get_defect_description(self, obj):
        return obj.defect_type.description


class CheckConfigSerializer(serializers.ModelSerializer):
    """
    数据序列化
    """
    model_info = serializers.SerializerMethodField()

    class Meta:
        model = models.CheckConfig
        fields = ['id', 'name', 'output', 'img_size',
                  'conf_thres', 'iou_thres', 'model_info']

    def get_model_info(self, obj):
        return obj.model_info.name


class CheckTypeSerializer(serializers.ModelSerializer):
    """
    数据序列化
    """

    class Meta:
        model = models.DefectType
        fields = ['id', 'name', 'video', 'image', 'type', 'description']

